/**
 * 校车历史轨迹-页面使用
 */

var url="trace/fenceInfo.do";  //列表展示url
var map = new BMap.Map("trace-map");
var point = new BMap.Point(116.404, 39.915);
map.centerAndZoom(point, 12);
map.enableScrollWheelZoom(true);


$(document).ready(function () {
	//初始化学校列表
	schoolList=syncGetSchool();
	//监听组织机构变化事件
	orgChangeListener();
});


function makeData(data){
	
	var path = data['filePath'];
	console.log("makeData-path:"+ basePath + path);
	
	var real_path = basePath + path;
	
	$.ajax({
		url: real_path,//json文件位置
		type: "GET",//请求方式为get
		dataType: "json", //返回数据格式为json
		success: function(data) {//请求成功完成后要执行的方法 
			console.log("success");
			loadMap(data);
		},
		complete: function(XMLHttpRequest, textStatus, data){
		    console.log("complete");
		}
	});
		
	/*$.ajax({
		url:listUrl, 
		type:"post",
		success:function(data) {
		    loanMap(data);
		}
    });*/
//	var data = new Array();
//	data[0] = {"lng":"113.569905","lat":"34.825496","id":"p1"};
//	data[1] = {"lng":"113.593405","lat":"34.813464","id":"p2"};
//	data[2] = {"lng":"113.624881","lat":"34.781154","id":"p3"};
//	loadMap(data);
}



/**
 * 加载地图
 * @param data
 * @returns
 */
function loadMap(data){
	//console.log(result);
	//var data = JSON.stringify(result);
	//var data = JSON.parse(result);
	console.log("loadMap:" + data);
	//Center从筛选条件中来
	var map = new BMap.Map("trace-map");
	var point = new BMap.Point(113.593405,34.813464);
	map.centerAndZoom(point, 15);
	map.enableScrollWheelZoom(true);
	var arr = [];
	//加入所有轨迹点
	for (var i = 0; i < data.length; i++){
		arr.push(new BMap.Point(data[i].lng,data[i].lat));
	}
		
	var driving = new BMap.DrivingRoute(map, { 
		onSearchComplete: function(results) {
			if (driving.getStatus() == BMAP_STATUS_SUCCESS) {
				var plan = driving.getResults().getPlan(0);
				var num = plan.getNumRoutes();
				for(var j =0 ; j < num ; j ++) {
					var pts= plan.getRoute(j).getPath();
					var polyline = new BMap.Polyline(pts);
					polyline.setStrokeColor("green");
					//polyline.setStrokeStyle("dashed");
					map.addOverlay(polyline);
				}
				/*for (var i = 0; i < data.length; i++){
					var myP = new BMap.Point(data[i].lng,data[i].lat)
					var m = new BMap.Marker(myP);
					var lab= new BMap.Label(data[i].id,{position:myP});
					map.addOverlay(m);
					map.addOverlay(lab);
				}*/
			}
		}
	}, {
		renderOptions : { map: map, autoViewport: true}
	});
	for (var i = 0; i < arr.length; i++) {
		driving.search(arr[i],arr[i+1]);
	}
	setTimeout(function(){
		map.setViewport(arr); 
	},100);//使涉及到的点都出现在视野
}



/**
 * 检索一条校车轨迹记录
 * 
 * @param param
 * @returns
 */
function queryTrace(param) {
	$.ajax({
		url : url,
		type : "post",
		data : param,
		success : function(data) {
			console.log(data);
			console.log("length:" + data.length);
			console.log("jiansuodao");
			for(var i = 0 ; i < data.length ; i ++) {
				var index_data = data[i];
				makeData(index_data);
			}
			
			
		}
	});

}


/**
 * 绑定查询事件
 * @returns
 */
$("#submit-search").click(function () {
	queryTrace(form2Json("fms")); //点击搜索
});